package com.example.administrator.activity.boradcast;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.activity.R;

public class BoradcastActivity extends AppCompatActivity implements View.OnClickListener {
    EditText ed_phone;
    Button btn;
    MyReceiver myReciver=new MyReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boradcast);
        ed_phone=findViewById(R.id.broadcast_ed_phone);
        btn=findViewById(R.id.broadcast_btn);
        btn.setOnClickListener(this);
        String[] s={"android.permission.PROCESS_OUTGOING_CALLS"};
        ActivityCompat.requestPermissions(BoradcastActivity.this,s,1);
    }

    @Override
    public void onClick(View v) {
        /*
        把输入框的鄂丽蓉保存在sp
         */
        String phone=ed_phone.getText().toString();
        SharedPreferences sp=getSharedPreferences("phone",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("phone",phone);
        editor.commit();
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    /**
     * Activity生命周期方法，自动调用
     * 注册广播
     */
    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        /*
        参数1:广播的接受者；
        参数2：订阅哪个广播
         */
        registerReceiver(myReciver,intentFilter);
    }


    /**
     * Activity生命周期方法，自动调用
     * 销毁广播
     */
    @Override
    protected void onPause() {
        super.onPause();
        /*
        本案例中使用动态注册广播的销毁的广播 不合适
         */
        unregisterReceiver(myReciver);
    }
}
